import type { RouteRecordRaw } from "vue-router"
import { routerConfig } from "@/router/config"
import { registerNavigationGuard } from "@/router/guard"
import { createRouter } from "vue-router"
import { flatMultiLevelRoutes } from "./helper"

const Layouts = () => import("@/layouts/index.vue")

/**
 * @name 常驻路由
 * @description 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置唯一的 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: ":path(.*)",
        component: () => import("@/pages/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/pages/error/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/pages/error/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/pages/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/",
    component: Layouts,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/Dashboard.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          svgIcon: "dashboard",
          affix: true
        }
      }
    ]
  },
  {
    path: '/material',
    name: 'MaterialManage',
    component: Layouts,
    meta: { title: '物料管理', svgIcon: 'material' },
    children: [
      {
        path: 'list',
        name: 'ListMaterial',
        component: () => import('@/views/material/MaterialList.vue'),
        meta: { title: '物料列表' }
      },
      {
        path: 'category',
        name: 'CategoryManage',
        component: () => import('@/views/material/MaterialCategory.vue'),
        meta: { title: '物料分类' }
      }
    ]
  },
  {
    path: '/supply',
    name: 'SupplyChainManage',
    component: Layouts,
    meta: { title: '采购与供应链管理', svgIcon: 'supply' },
    children: [
      {
        path: 'vendor',
        name: 'VendorManage',
        component: () => import('@/views/supply/VendorManage.vue'),
        meta: { title: '供应商管理' }
      },
      {
        path: 'purchase-in',
        name: 'PurchaseInManage',
        component: () => import('@/views/supply/PurchaseInManage.vue'),
        meta: { title: '采购入库' }
      },
      {
        path: 'plan',
        name: 'PurchasePlanManage',
        component: () => import('@/views/supply/PurchasePlanManage.vue'),
        meta: { title: '采购计划与跟踪' }
      },
      {
        path: 'vendor-evaluate',
        name: 'VendorEvaluate',
        component: () => import('@/views/supply/VendorEvaluate.vue'),
        meta: { title: '供应商评价与对比' }
      }
    ]
  },
  {
    path: '/manufacture',
    name: 'ManufactureManage',
    component: Layouts, 
    meta: { title: '生产流程管理', svgIcon: 'manufacture' },
    children: [
      {
        path: 'manufactureMaterial',
        name: 'ProduceMaterial',
        component: () => import('@/views/manufacture/ProduceMaterial.vue'),
        meta: { title: '生产领料' }
      },
      {
        path: 'process',
        name: 'ProduceProcess',
        component: () => import('@/views/manufacture/ProduceProcess.vue'),
        meta: { title: '生产工序' }
      },
      {
        path: 'schedule',
        name: 'ProduceSchedule',
        component: () => import('@/views/manufacture/ProduceSchedule.vue'),
        meta: { title: '生产排产' }
      },
      {
        path: 'process-progress',
        name: 'ProcessProgress',
        component: () => import('@/views/manufacture/ProcessProgress.vue'),
        meta: { title: '工序进度跟踪' }
      },
      {
        path: 'kanban',
        name: 'ProduceKanban',
        component: () => import('@/views/manufacture/ProduceKanban.vue'),
        meta: { title: '任务看板' }
      }
    ]
  },
  {
  path: '/quality-inventory',
  name: 'QualityInventory',
  component: Layouts, 
  meta: { title: '质量与库存管理', svgIcon: 'quality-inventory' },
  children: [
    {
      path: 'incoming-inspection',
      name: 'IncomingInspection',
      component: () => import('@/views/quality-inventory/IncomingInspection.vue'),
      meta: { title: '来料检验' }
    },
    {
      path: 'return-material',
      name: 'ReturnMaterial',
      component: () => import('@/views/quality-inventory/ReturnMaterial.vue'),
      meta: { title: '生产退料' }
    },
    {
      path: 'stock-monitor',
      name: 'StockMonitor',
      component: () => import('@/views/quality-inventory/StockMonitor.vue'),
      meta: { title: '库存实时监控' }
    },
    {
      path: 'stock-warning',
      name: 'StockWarning',
      component: () => import('@/views/quality-inventory/StockWarning.vue'),
      meta: { title: '库存预警与通知' }
    },
    {
      path: 'quality-issues',
      name: 'QualityIssues',
      component: () => import('@/views/quality-inventory/QualityIssues.vue'),
      meta: { title: '质量问题记录' }
    }
  ]
},
{
  path: '/report',
  name: 'ReportCenter',
  component: Layouts, 
  meta: { title: '数据分析与报表中心', svgIcon: 'report-center' },
  children: [
    {
      path: 'multi-dim-report',
      name: 'MultiDimReport',
      component: () => import('@/views/report/MultiDimReport.vue'),
      meta: { title: '多维统计报表' }
    },
    {
      path: 'trend-charts',
      name: 'TrendCharts',
      component: () => import('@/views/report/TrendCharts.vue'),
      meta: { title: '趋势分析图表' }
    }
  ]
}



]

/**
 * @name 动态路由
 * @description 用来放置有权限 (Roles 属性) 的路由
 * @description 必须带有唯一的 Name 属性
 */
export const dynamicRoutes: RouteRecordRaw[] = [

]

/** 路由实例 */
export const router = createRouter({
  history: routerConfig.history,
  routes: routerConfig.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  try {
    // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    location.reload()
  }
}

// 注册路由导航守卫
registerNavigationGuard(router)
